//
//  StatusFrame.m
//  CMJWeibo
//
//  Created by BH on 15/12/29.
//
//

#import "StatusFrame.h"
#import "StatusModel.h"
#import "UserInfo.h"
#import "CMJCommon.h"

@implementation StatusFrame

/**
 *  根据数据模型计算子控件的frame
 */
-(void)setStatus:(StatusModel *)status
{
    _status = status;
    
    //计算顶部控件的frame
    [self setTopViewFrame];
    
    //计算底部工具条的frame
    [self setToolbarFrame];
    
    //cell的高度
    _cellHeight = CGRectGetMaxY(_toolbarF);
}

/**
 *  顶部控件的frame
 */
-(void)setTopViewFrame
{
    //计算原微博frame
    [self setOriginalFrame];
    
    //计算转发微博frame
    [self setRepostFrame];
    
    //计算顶部View frame
    StatusModel *status = self.status.retweeted_status;
    CGFloat topViewH = 0;
    if (status) {
        //有转发微博
        topViewH = CGRectGetMaxY(self.repostViewF);
    }else{
        //没有转发微博
        topViewH = CGRectGetMaxY(self.originalViewF);
    }
    CGFloat topViewX = 0;
    CGFloat topViewY = CellMargin;
    CGFloat topViewW = ScreenWidth;
    _topViewF = CGRectMake(topViewX, topViewY, topViewW, topViewH);
}

/**
 * 原微博frame
 */
-(void)setOriginalFrame
{
    //头像
    CGFloat iconX = CellPadding;
    CGFloat iconY = CellPadding;
    CGFloat iconW = 35;
    CGFloat iconH = 35;
    _originalIconF = CGRectMake(iconX, iconY, iconW, iconH);
    
    //昵称
    CGFloat nameX = CGRectGetMaxX(_originalIconF)+CellPadding;
    CGFloat nameY = iconY;
    CGSize nameSize = [self.status.user.name sizeWithFont:NicknameFont];
    _originalNameF = (CGRect){{nameX,nameY},nameSize};
    
    //会员图标
    CGFloat vipX = CGRectGetMaxX(_originalNameF)+CellPadding;
    CGFloat vipY = nameY;
    CGFloat vipW = 14;
    CGFloat vipH = nameSize.height;
    _originalVipF = CGRectMake(vipX, vipY, vipW, vipH);
    
    //正文
    CGFloat contentX = iconX;
    CGFloat contentY = CGRectGetMaxY(_originalIconF)+CellPadding;
    CGFloat contentMaxW = ScreenWidth-CellPadding*2;
    CGSize contentMaxSize = CGSizeMake(contentMaxW, MAXFLOAT);
    CGSize contentSize = [self.status.text sizeWithFont:ContentFont constrainedToSize:contentMaxSize];
    _originalContentF = (CGRect){{contentX,contentY},contentSize};
    
    //配图
    NSInteger photosCount = self.status.pic_urls.count;
    CGFloat originalViewH = 0;
    if (photosCount) {
        //有配图
        CGFloat photosX = contentX;
        CGFloat photosY = CGRectGetMaxY(_originalContentF)+CellPadding;
        CGSize photosSize = [self photosSizeWithCount:photosCount];
        _originalPhotosF = (CGRect){{photosX,photosY},photosSize};
        
        originalViewH = CGRectGetMaxY(_originalPhotosF)+CellPadding;
    }else{
        //没有配图
        originalViewH = CGRectGetMaxY(_originalContentF)+CellPadding;
    }
    
    //原微博整体的frame
    _originalViewF = CGRectMake(0, 0, ScreenWidth, originalViewH);
}

/**
 *  计算配图的尺寸
 */
-(CGSize)photosSizeWithCount:(NSInteger)count
{
    //一行最多显示的列数
    NSInteger maxCols = (count == 4)?2:3;
    
    //列数
    NSInteger cols = (count>=maxCols)?maxCols:count;
    
    //行数
    NSInteger rows = count/maxCols;
    if (count%maxCols != 0) {
        rows++;
    }
    
    // 配图的宽度 == 列数 * 图片的宽 + (列数 - 1) * 图片之间的间距
    CGFloat photosW = cols*PhotoWidth + (cols-1) * PhotoMargin;
    
    // 配图的高度 == 行数 * 图片的高 + (行数 - 1) * 图片之间的间距
    CGFloat photosH = rows*PhotoHeight + (rows-1) *PhotoMargin;
    
    return CGSizeMake(photosW, photosH);
}

/**
 * 转发微博frame
 */
-(void)setRepostFrame
{
    StatusModel *repostedStatus = self.status.retweeted_status;
    if (!repostedStatus) {
        //没有转发微博
        return;
    }
    
    //昵称
    CGFloat repostedNameX = CellPadding;
    CGFloat repostedNameY = CellPadding;
    NSString *repostName = [NSString stringWithFormat:@"@%@",repostedStatus.user.name];
    CGSize repostedSize = [repostName sizeWithFont:NicknameFont];
    _repostNameF = (CGRect){{repostedNameX,repostedNameY},repostedSize};
    
    //正文
    CGFloat repostedContentX = repostedNameX;
    CGFloat repostedContentY = CGRectGetMaxY(_repostNameF)+CellPadding;
    CGFloat repostedContentMaxW = ScreenWidth-CellPadding*2;
    CGSize repostedContentMaxSize = CGSizeMake(repostedContentMaxW, MAXFLOAT);
    CGSize repostedContentSize = [repostedStatus.text sizeWithFont:ContentFont constrainedToSize:repostedContentMaxSize];
    _repostContentF = (CGRect){{repostedContentX,repostedContentY},repostedContentSize};
    
    //配图
    NSInteger repostedPhotosCount = self.status.retweeted_status.pic_urls.count;
    CGFloat repostedViewH = 0;
    if (repostedPhotosCount) {
        //有配图
        CGFloat repostedPhotosX = repostedNameX;
        CGFloat repostedPhotosY = CGRectGetMaxY(_repostContentF)+CellPadding;
        CGSize repostedPhotosSize = [self photosSizeWithCount:repostedPhotosCount];
        _repostPhotosF = (CGRect){{repostedPhotosX,repostedPhotosY},repostedPhotosSize};
        
        repostedViewH = CGRectGetMaxY(_repostPhotosF)+CellPadding;
        
    }else{
        //没有配图
        repostedViewH = CGRectGetMaxY(_repostContentF)+CellPadding;
    }
    
    //转发微博整体frame
    _repostViewF = CGRectMake(0, CGRectGetMaxY(_originalViewF), ScreenWidth, repostedViewH);
}

/**
 *  底部工具条的frame
 */
-(void)setToolbarFrame
{
    CGFloat toolbarX = 0;
    CGFloat toolbarY = CGRectGetMaxY(_topViewF);
    CGFloat toolbarW = ScreenWidth;
    CGFloat toolbarH = 35;
    
    _toolbarF = CGRectMake(toolbarX, toolbarY, toolbarW, toolbarH);
}

@end
